package cn.maihe.elg.operation.web.order;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.enums.AcceptOrderStatus;
import cn.maihe.elg.operation.model.enums.GuaranteeCategory;
import cn.maihe.elg.operation.model.enums.GuaranteeType;
import cn.maihe.elg.operation.model.enums.RefundOrderStatus;
import cn.maihe.elg.operation.model.vo.order.*;
import cn.maihe.elg.operation.repository.entity.RefundOrderInfoDO;
import cn.maihe.elg.operation.service.order.GuaranteeProgressInfoService;
import cn.maihe.elg.operation.service.order.RefundOrderInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import cn.maihe.elg.operation.utils.DownloadExcelTool;
import cn.maihe.elg.operation.utils.StringUtil;
import cn.maihe.framework.starter.base.sql.PageBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 我的售后(退款)订单管理
 * @Author WJH
 * @Date 2021/05/06
 */
@Api(value = "我的售后(退款)订单管理", tags = "我的售后(退款)订单管理")
@RestController
@RequestMapping("/api")
public class RefundOrderInfoController {

    @Resource
    private RefundOrderInfoService refundOrderInfoService;
    @Resource
    private GuaranteeProgressInfoService guaranteeProgressInfoService;
    @Resource
    private DownloadExcelTool downloadExcelTool;
    @Resource
    private ElgResourceService elgResourceService;

    @ApiOperation(value = "分页查询我的售后(退款)信息", notes = "根据我的售后(退款) 录入参数 分页查询")
    @RequestMapping(value = "/refundOrders", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "centerOrderId", value = "中心订单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "refundOrderNo", value = "售后(退款)号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "bidCustomerName", value = "申请企业名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sectionName", value = "标段名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "applyOrderStatus", value = "订单状态", dataType = "int", paramType = "query", allowableValues = "0,1,2,3,4,5"),

    })
    public PageBo<ListRefundOrderDO> pageList(int pageNo, int pageSize, ListRefundOrderVO vo) {
        return refundOrderInfoService.selectPageBySql(pageNo, pageSize, vo);
    }

    @ApiOperation(value = "获取我的售后(退款)信息", notes = "根据我的售后(退款)信息id获取我的售后(退款)信息")
    @ApiImplicitParam(name = "id", value = "我的售后(退款)信息id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/refundOrder/{id}", method = RequestMethod.GET)
    public RefundOrderDetailVO getApplyOrderDetailById(@PathVariable Long id) {
        RefundOrderDetailVO curVO = refundOrderInfoService.getRefundOrderDetailById(id);
        if (curVO == null) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "我的售后(退款)信息id");
        }
        return curVO;
    }

    @ApiOperation(value = "订单状态审核", notes = "0待退款 1待复审 2已退款 3已拒绝 4已取消 ")
    @ApiImplicitParam(name = "id", value = "我的售后(退款)信息id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/refundOrder/auditStatus", method = RequestMethod.PUT)
    public Boolean refundOrderStatusOperate(@Validated @RequestBody RefundOrderStatusOperateVO vo) {

        RefundOrderInfoDO curDO = refundOrderInfoService.selectById(vo.getId());
        if (curDO == null) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "我的售后(退款)信息id");
        }
        if (curDO.getStatus() == null || curDO.getStatus().intValue() != vo.getStatus().intValue()) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "原我的售后(退款)状态与库表状态不一致");
        }
        RefundOrderStatus status = RefundOrderStatus.resolve(vo.getStatus().intValue());

        switch (status) {
            case UNREFUND: {
                //待退款  退款初审操作
                return refundOrderInfoService.firstAudit(curDO,vo);
            }
            case APPROVAL: {
                //待复审：复审操作
                return refundOrderInfoService.confirmAudit(curDO,vo);
            }
            default: {
                throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "暂不支持该状态" + status + "操作");
            }
        }
    }


    @RequestMapping(value = "/refundOrder/download", method = RequestMethod.POST)
    public void downloadApplyOrder(HttpServletResponse response, @RequestBody ListRefundOrderVO vo) {
        PageBo<ListRefundOrderDO> pageBo = refundOrderInfoService.selectPageBySql(1, DownloadExcelTool.maxRowNum, vo);
        List<ListRefundOrderDO> list = pageBo.getList().stream().map(l -> {
            l.setGuaranteeTypeName(GuaranteeType.getDescByType(l.getGuaranteeType()));
            l.setGuaranteeCategoryName(GuaranteeCategory.getDescByType(l.getGuaranteeCategory()));
            l.setStatusName(AcceptOrderStatus.getDescByStatus(l.getStatus()));
            l.setGuaranteeFeeYuan(StringUtil.toYuan(l.getGuaranteeFee()));
            l.setBidBondYuan(StringUtil.toYuan(l.getBidBond()));
            return l;
        }).collect(Collectors.toList());
        downloadExcelTool.downloadByTemplateName("refundOrderReport.xlsx", "我的售后订单", response, list, null);
    }

}
